package com.wnhz.work.jy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.HomeFragment1.RecommendBean;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/9/1 0001.
 */
public class GridViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    private List<RecommendBean> homeList;
    private int screenWidth;

    public GridViewAdapter(Context context, List<RecommendBean> homeList, int screenWidth) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.homeList = homeList;
        this.screenWidth = screenWidth;

    }

    @Override
    public int getCount() {
        return homeList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.homefrag1_item, null);
            holder.mImg = (ImageView) convertView.findViewById(R.id.item_home_recycle1_image);
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.name = (TextView) convertView.findViewById(R.id.item_home_recycle1_name);
            holder.price = (TextView) convertView.findViewById(R.id.item_home_recycle1_price);
            holder.type = (TextView) convertView.findViewById(R.id.tv_type);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        int width = screenWidth  / 2;
//
//        ViewGroup.LayoutParams params = holder.mImg.getLayoutParams();
//
//        params.width = width;
//        params.height = width;

//        holder.mImg.setLayoutParams(params);
        holder.price.setText("¥"+homeList.get(position).getGoodPrice());
        holder.name.setText(homeList.get(position).getGoodName());
        x.image().bind(holder.mImg,homeList.get(position).getGoodImg());
        if ("1".equals(homeList.get(position).getIs_presale())){
            if (holder.img.getVisibility() == View.GONE){
                holder.img.setVisibility(View.VISIBLE);
            }
        }else {
            if (holder.img.getVisibility() == View.VISIBLE){
                holder.img.setVisibility(View.GONE);
            }
        }
        //ImageLoader.getInstance().displayImage(homeList.get(position).getGoodImg(),holder.mImg);
        /*holder.mImg.setImageResource(Integer.parseInt(homeList.get(position).getImg()));
        holder.name.setText(homeList.get(position).getTitle());
        holder.price.setText("¥"+homeList.get(position).getPrice());*/
//        holder.type.setText(homeList.get(position).getType());
        return convertView;
    }

    private class ViewHolder {
        View layout, line;
        ImageView mImg;
        TextView name;
        TextView price;
        TextView type;
        ImageView img;
    }
}
